package org.wby.jwt.utils;


import com.google.gson.Gson;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wby.jwt.config.props.SecurityProperties;
import org.wby.jwt.enums.SecutityEnums;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;

@Component
public class JWTUtil {
    @Autowired
    private  SecurityProperties securityProperties;

    private static final String SEC_KEY="kcBQ626IJ608BNQ0GQmWIIsdiF4Xaw==";

//    public JWTUtil(SecurityProperties securityProperties) {
//        this.securityProperties = securityProperties;
//    }


    public  String createJWT() {

        HashMap<String, String> headerParam = new HashMap<>();
        headerParam.put("alg", securityProperties.getAlg());
        headerParam.put("typ",securityProperties.getTyp());


        long currentTimeMillis = System.currentTimeMillis();
        HashMap<String, Object> claimsParam = new HashMap<>();
        //jwt签发者
        claimsParam.put("iss", securityProperties.getIss());
        //jwt所面向的用户
        claimsParam.put("sub", "user");
        claimsParam.put("aud", "user");//接收jwt的一方,LinkedHashSet
        claimsParam.put("exp",currentTimeMillis+securityProperties.getExp());//jwt的过期时间，这个过期时间必须要大于签发时间
        //claimsParam.put("nbf",currentTimeMillis);//nbf: 定义在什么时间之前，该jwt都是不可用的.
        claimsParam.put("iat", currentTimeMillis);//iat: jwt的签发时间
        return Jwts.builder().header().add(headerParam).and()
                .claims(claimsParam)
                .signWith(Keys.hmacShaKeyFor(SEC_KEY.getBytes(StandardCharsets.UTF_8)))
                .expiration(new Date(currentTimeMillis+(securityProperties.getExp())))
                .compact();
    }

    public String  parseJwt(String jwt,SecutityEnums key) {
        SecretKey secretKey = Keys.hmacShaKeyFor(SEC_KEY.getBytes(StandardCharsets.UTF_8));
        JwtParser jwtParser = Jwts.parser().verifyWith(secretKey).build();
        Jws<Claims> claimsJws = jwtParser.parseSignedClaims(jwt);
         return claimsJws.getPayload().get(key.getValue()).toString();
    }

    public <T> T parseJwt(String jwt, Class<T> classOfT) {
        String parseJwt = parseJwt(jwt, SecutityEnums.USER_CONTEXT);
        return new Gson().fromJson(parseJwt, classOfT);
    }

    public String testPropers() {
        return securityProperties.getSecKey();

    }


    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {


    }


//    public static SecurityProperties getSecurityProperties() {
//        return securityProperties;
//    }
//
//    public static void setSecurityProperties(SecurityProperties securityProperties) {
//        JWTUtil.securityProperties = securityProperties;
//    }
}
